package com.leetcode.stack_queue;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author Dennis Li
 * @date 2020/7/24 19:42
 */
public class MinStack {
    private final Deque<Integer> stack;
    private final Deque<Integer> minStack;
    private int min = Integer.MAX_VALUE;

    public MinStack() {
        stack = new LinkedList<>();
        minStack = new LinkedList<>();
    }

    // 用一个小堆栈，只push较小的值
    public void push(int x) {
        stack.push(x);
        min = Math.min(x, min);
        minStack.push(min);
    }

    public void pop() {
        stack.pop();
        minStack.pop();
        min = getMin();
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }


}
